package com.lei.androidheight_four.presenter;

import android.util.Log;

import com.google.gson.Gson;
import com.lei.androidheight_four.bean.LoginBean;
import com.lei.androidheight_four.bean.RegisterBean;
import com.lei.androidheight_four.contract.LoginContract;
import com.lei.mvpmodel.presenter.BasePresenter;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

public class LoginPresenter extends BasePresenter<LoginContract.Model, LoginContract.View> implements LoginContract.Presenter {
    public LoginPresenter(LoginContract.Model mmodel, LoginContract.View mview) {
        super(mmodel, mview);
    }

    @Override
    public void login(String name, String pwd) {
        mmodel.login(name, pwd, new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                String body = response.body();
                if (!body.equals(null)){
                    LoginBean loginBean = new Gson().fromJson(body, LoginBean.class);
                    int errorCode = loginBean.getErrorCode();
                    if (errorCode == 0){
                        mview.gomain("登陆成功");
                        Log.e("ljl", "onSuccess: "+"登陆成功" );
                    }
                }
            }
        });
    }

    @Override
    public void register(String name, String pwd) {
        mmodel.register(name, pwd, new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                String body = response.body();
                if (!body.equals(null)){
                    RegisterBean registerBean = new Gson().fromJson(body, RegisterBean.class);
                    int errorCode = registerBean.getErrorCode();
                    if (errorCode==0){
                        mview.gomain("注册成功");
                        Log.e("ljl", "onSuccess: "+"注册成功" );
                    }
                }
            }
        });
    }

    @Override
    public void destroy() {

    }
}
